package com.alex.service.Search_Plans;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MultiMatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;

import java.util.Set;

import static com.alex.service.Search_Plans.SearchPlanUtils.enWordIgnore;
import static com.alex.utils.MyUtils.zsout;


/**
 * @Author: 张福垒
 * @Date: 2024/11/27 16:32
 * @Description: [全部] 分词检索
 * @phone: 13864677931
 */
public class EN_CN_Match_query {
    /**
     * 针对主题的 分词的检索方法 根据输入的单次或者汉字长度来判断需要命中X/Y 或者 X% 之多少的数字
     *
     * @param freeWord
     * @return
     */
    public static Query en_cn_match_query(String freeWord, String cnField, String enField,boolean printLog) {
        int wordCount = freeWord.length();  //默认算中文检索词的字数
        String minimumMatch;
        //首先判断中英文混合输入的情况
        if (SearchPlanUtils.isContainChineseAndEnglish(freeWord)) {
            zsout("[双字段][混合][CN][EN][" + wordCount + "]   " + freeWord,printLog);
            if (wordCount <= 10) {
                minimumMatch = "99%";
            } else {
                String finalFreeWord2 = freeWord;
                BoolQuery.Builder allQueryBuilder = new BoolQuery.Builder();
                allQueryBuilder.should(
                        i -> i.match(
                                j -> j.field(enField).query(finalFreeWord2)
                        )
                );
                allQueryBuilder.should(
                        i -> i.match(
                                j -> j.field(cnField).query(finalFreeWord2)
                        )
                );
                return allQueryBuilder.build()._toQuery();
            }
            String finalFreeWord = freeWord;
            return MultiMatchQuery.of(i -> i.fields(
                    cnField, enField

            ).query(finalFreeWord).minimumShouldMatch(minimumMatch))._toQuery();
        }
        /**
         * 只有中文或者只有英文的时候
         */
        Set<String> set = enWordIgnore();
        for (String oneWord : set) {
            if (freeWord.contains(" " + oneWord + " ")) {
                freeWord = freeWord.replaceAll(" " + oneWord + " ", " ");
                freeWord = freeWord.replaceAll(" +", " ").trim();
            }
        }
        if (!SearchPlanUtils.isContainChinese(freeWord)) {//不过不包含中文就算单词个数
            wordCount = freeWord.split("\\s+").length;
            zsout("[双字段][分词][EN][" + wordCount + "]   " + freeWord,printLog);
        } else {
            zsout("[双字段][分词][CN][" + wordCount + "]   " + freeWord,printLog);
        }
        switch (wordCount) {
            case 1:
                minimumMatch = "100%";
                break;
            case 2:
                minimumMatch = "100%";
                break;
            case 3:
                minimumMatch = "100%";
                break;
            case 4:
                minimumMatch = "100%";
                break;
            default:
                minimumMatch = "80%";
                break;
        }
        String finalFreeWord = freeWord;
        return MultiMatchQuery.of(i -> i.fields(
                cnField, enField
        ).query(finalFreeWord).minimumShouldMatch(minimumMatch))._toQuery();
    }


}
